import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置不需要加载转圈
Nprogress.configure({
  showSpinner: false
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/Login/index.vue'), meta: { title: '登录' } },
    {
      path: '/', component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home', 
          component: () => import('@/views/Home/index.vue'), 
          meta: { title: '首页' } 
        },
        {
          path: '/article', 
          component: () => import('@/views/Article/index.vue'), 
          meta: { title: '健康咨询' } 
        },
        {
          path: '/notify', 
          component: () => import('@/views/Notify/index.vue'), 
          meta: { title: '消息通知' } 
        },
        {
          path: '/user', 
          component: () => import('@/views/User/index.vue'), 
          meta: { title: '我的' } 
        },
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '快速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情详情' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      beforeEnter(to){
        if(to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/User/ConsultPage.vue'),
      meta: { title: '我的问诊' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/User/components/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/Order/OrderPage.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/Order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/Order/OrderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/Order/OrderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    }
  ]
})

// 访问拦截
router.beforeEach((to) => {
  // 进度条添加
  Nprogress.start()
  // 修改标题
  document.title = `优医问诊-${to.meta.title}`
  // 如果返回 true 或 无参数时 就是放行
  // 拦截到某个页面 返回 '路由地址'
  const store = useUserStore()
  const whiteList = ['/login', '/login/callback']  //可以放置更多的白名单页面
  // 需求：当你没有登录也就是没有token 且 你访问的不是登陆页面 拦截到登录
  if (!store.user?.token && !whiteList.includes(to.path)) return '/login'
})
// 路由切换结束
router.afterEach((to) => {
  document.title = `优医问诊-${to.meta.title}`
  Nprogress.done()
})

export default router
